package org.qs.webservice.controller;


import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.apache.ibatis.annotations.Param;
import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.AuthorService;
import org.qs.webservice.service.BookReviewService;
import org.qs.webservice.service.UserConsumeService;
import org.qs.webservice.service.UserService;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.StringUtils;
import org.springframework.stereotype.Component; 


/**
 * 获取某个区块中素材列表接口
 * @ClassName: BlockRestController 
 * @author xusaike 
 * @date 2015年9月28日 下午2:39:54 
 *
 */
@Component
@Path("/v{version}/author")
public class AuthorRestController {

	@Resource
	AuthorService authorService;

	@GET  
	@Path("collect")
	@Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)  
	public String getCollect(@QueryParam("pages")  int pages,
			@QueryParam("author_id")String author_id,
			@QueryParam("contents")  int contents,
			@HeaderParam("Info-Uuid")String device_identifier,
			@HeaderParam("token")String token){  
		try{
			device_identifier=IReadUtils.preventInjection(device_identifier);
			author_id=IReadUtils.preventInjection(author_id);
			if(StringUtils.isNullOrEmpty(device_identifier)){
				device_identifier = "";
			}
			String str= authorService.getCollect(author_id,pages,contents,token,device_identifier);  
			return str;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "获取收藏数据异常！",errorJson);
		}
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		tokenMap.put("code", 403);
		tokenMap.put("reason","服务器错误");
		return IReadUtils.beanToJson(tokenMap);
	}  
	@GET  
	@Path("works")
	@Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)  
	public String getWorks(@QueryParam("pages")  int pages,
			@QueryParam("author_id")String author_id,
			@QueryParam("contents")  int contents,
			@HeaderParam("Info-Uuid")String device_identifier,
			@HeaderParam("token")String token){  
		try{
			device_identifier=IReadUtils.preventInjection(device_identifier);
			author_id=IReadUtils.preventInjection(author_id);
			if(StringUtils.isNullOrEmpty(device_identifier)){
				device_identifier = "";
			}
			String str= authorService.getWorks(author_id,pages,contents,token,device_identifier);  
			return str;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "获取收藏数据异常！",errorJson);
		}
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		tokenMap.put("code", 403);
		tokenMap.put("reason","服务器错误");
		return IReadUtils.beanToJson(tokenMap);
	}  
	@GET  
	@Path("reviews")
	@Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)  
	public String getReviews(@QueryParam("pages")  int pages,
			@QueryParam("author_id")String author_id,
			@QueryParam("contents")  int contents,
			@HeaderParam("Info-Uuid")String device_identifier,
			@HeaderParam("token")String token){  
		try{
			device_identifier=IReadUtils.preventInjection(device_identifier);
			author_id=IReadUtils.preventInjection(author_id);
			if(StringUtils.isNullOrEmpty(device_identifier)){
				device_identifier = "";
			}
			String str= authorService.getReviews(author_id,pages,contents,token,device_identifier);  
			return str;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "获取收藏数据异常！",errorJson);
		}
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		tokenMap.put("code", 403);
		tokenMap.put("reason","服务器错误");
		return IReadUtils.beanToJson(tokenMap);
	}  
	
}